"""

.. module:: support_service
.. moduleauthor:: @author: 

*Description*:

This module contains the SupportService class.

*Constants*:

.. seealso:: class :class:`runtime.service.Service`
"""

# Imports
#
import datetime
import threading
import time
import logging

from datastore.resource import *
from datastore.uri import *
from runtime.service import *

# Constants
#

# Classes
#
class SupportService(Service):
    """
    **Constants**:
    """
    
    def cb_start(self, uri, action, res):
        """
        :param uri: Uri
        :param action:
        :param res: Resource
        """
        
        self.logger.log(logging.INFO, "Service (%s) starting.", self.title)
        self.spt = self.async(self.cb_async_tests, uri, action, res, 
                              name="async tests")
        
        # some test data for HTTP
        res = self.spaces.get(Uri('internal:base/node_configuration'))
        self.spaces.put(Uri('external:node_configuration'), res)
        
        res = Resource(type='term:base/services/http/response')
        res.content_type = "text/html"
        res.content = """
            <h1>Admin Index</h1>\n
            <a href="/admin/spaces/">Browse Spaces</a><br />\n
            <a href="/admin/spaces-full_list">List all Resources in Spaces</a><br />\n
            <a href="/admin/datastore-full_list">List all Resources in the DataStore</a><br />\n
            <a href="/admin/restart">Restart the Node</a><br />\n
            """
        res.content_length = len(res.content)
        self.spaces.put(Uri('external:admin'), res)

    def cb_async_tests(self, uri, action, res):
        """
        :param uri: Uri
        :param action:
        :param res: Resource
        """
        
        # test access to options.
        self.logger.log(logging.INFO, "Test option (%s)", res.options.test)
           
        # test async callbacks.
        self.async(self.cb_async_cb, Uri('ccc'), 'get', 
                            Resource(type='type.t.b.a'),
                            name="async cb")

        # time_task tests
        timestamp_res = self.spaces.get(
                    Uri('internal:base/services/time_service/timestamp'))
        if timestamp_res is None:
            self.logger.log(logging.INFO, "time_task:timestamp not available.")
        else:
            self.logger.log(logging.INFO, "Timestamp1 = %s",
                            timestamp_res.timestamp)
            
        timer_res = Resource(type='term:base/services/time/timer')
        timer_res.interval = 0.5
        #res.timestamp = "2010-05-05 12:10:00"
        timer_res.iterations = 2
        timer_res.set_var('dc:title', "A test timer.")
        new_uri = self.spaces.request(
            Uri('internal:base/services/time_service/timer/'), timer_res,
            self.cb_timer_pop)
        #time.sleep(1)
        #self.logger.log(logging.INFO, "delete timer.")
        #self.spaces.delete(new_uri)
        
        time.sleep(2)
        self.logger.log(logging.INFO, "-" * 40)

        # list threads
        for thread in threading.enumerate():
            self.logger.log(logging.INFO, "Thread %s (daemon=%s)",
                            thread.name, thread.daemon)
        self.logger.log(logging.INFO, "-" * 40)

        # print stuff from spaces.
        for u in self.spaces.find(Uri()):
            self.logger.log(logging.DEBUG, "Spaces URI %s", str(u.shorten()))
        self.logger.log(logging.DEBUG, "-" * 40)
            
#        # shutdown cleanly
#        time.sleep(999)
#        control_res = Resource(type='term:base/runtime/node_control')
#        control_res.action = 'stop'
#        self.spaces.put(Uri('internal:base/node_control'), control_res)
        

    def cb_timer_pop(self, uri, action, res):
        """
        :param uri: Uri
        :param action:
        :param res: Resource
        """
        
        self.logger.log(logging.INFO, "Pop.")
        # delete the timer pop resource (one is created for each pop).
        self.spaces.delete(uri)

    def cb_async_cb(self, uri, action, res):
        """
        :param uri: Uri
        :param action:
        :param res: Resource
        """
        
        self.logger.log(logging.INFO, "Async test (%s).", uri)
        #time.sleep(5)
